package org.example.sys.ui.dashboard

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import org.example.sys.R
import org.example.sys.bean.Book
import org.example.sys.databinding.FragmentDetailBinding

class DetailFragment : Fragment(), OnClickListener {

    private var _binding: FragmentDetailBinding? = null
    private val detailViewModel by lazy { ViewModelProvider(this)[DetailViewModel::class.java] }

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = DataBindingUtil.inflate(inflater, R.layout.fragment_detail, container, false)
        val root: View = binding.root
        detailViewModel.unLock.set(true)
        detailViewModel.book.value = Book(0, null, null, null, 0)
        this.arguments?.apply {
            val book = getParcelable(TAG) as Book?
            book?.apply {
                detailViewModel.book.value = book
                binding.btnSubmit.text = getString(R.string.btn_text_confirm)
            }
        }
        binding.detailViewModel = detailViewModel
        binding.btnSubmit.setOnClickListener(this)
        binding.btnReset.setOnClickListener(this)
        binding.lifecycleOwner = this
        lifecycleScope.launch {
            detailViewModel.detailLiveData.observe(viewLifecycleOwner) { state ->
                when (state) {
                    DetailViewModel.State.SaveSuccess -> {
                        setDetail(State.Reset)
                        Toast.makeText(
                            context,
                            getString(R.string.tip_save_success),
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                    DetailViewModel.State.UpdateSuccess -> {
                        setDetail(State.Reset)
                        Toast.makeText(
                            context,
                            getString(R.string.tip_update_success),
                            Toast.LENGTH_LONG
                        ).show()
                    }

                    DetailViewModel.State.Failed -> {
                        setDetail(State.UnLock)
                        Toast.makeText(
                            context,
                            getString(R.string.tip_save_or_update_failed),
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                    else -> {
                        setDetail(State.UnLock)
                        Toast.makeText(
                            context,
                            getString(R.string.title_some_things_wrong),
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                }
            }
        }
        return root
    }

    private fun setDetail(state: State) {
        binding.detailViewModel?.apply {
            when (state) {
                State.Lock -> {
                    unLock.set(false)
                }

                State.UnLock, State.Reset -> {
                    unLock.set(true)
                    if (state == State.UnLock) return
                    book.value = Book(0, null, null, null, 0)
                }
            }
        }

    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onClick(v: View?) {
        v?.apply {
            when (id) {
                R.id.btn_submit -> {
                    binding.detailViewModel?.book?.value?.apply {
                        if (title.isNullOrEmpty()) {
                            Toast.makeText(
                                context,
                                getString(R.string.tip_save_or_update_failed_title),
                                Toast.LENGTH_SHORT
                            ).show()
                            return
                        }
                        setDetail(State.Lock)
                        detailViewModel.saveOrUpdate(this)
                    }
                }

                R.id.btn_reset -> {
                    setDetail(State.Reset)
                }
            }
        }
    }

    companion object {
        const val TAG = "DETAIL"
    }

    enum class State {
        Lock, UnLock, Reset
    }
}